<?php

namespace Drinkiit\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Drinkiit\BlogBundle\Form\AdminArticleType;
use Drinkiit\BlogBundle\Handler\DefaultHandler;
use Drinkiit\BlogBundle\Entity\Article;

class AdminArticleController extends Controller
{
	public function em($objet) {
		return $this->getDoctrine()->getEntityManager()->getRepository($objet) ;
	}
	
	public function indexAction()
	{
		$produit = new Article();
		$form = $this->createForm(new AdminArticleType, $produit);
		
        $formHandler = new DefaultHandler($form, $this->get('request'), $this, $this->getDoctrine()->getEntityManager());
		
        if( $formHandler->process() )
        {
			$this->get('logger')->info("[INFO] Formulaire bien validé, d'ou la redirection [admin_management_article]") ;
            return $this->redirect( $this->generateUrl('admin_management_article') );
        }
		
		$liste_article = $this->em('DrinkiitBlogBundle:Article')->findAll() ;
		$this->get('logger')->info("[INFO] Nombre d'articles : ".count($liste_article)) ;
		return $this->render('DrinkiitBlogBundle:Admin:Article.html.twig', array(
			'form' => $form->createView(),
			'titre' => 'Ajouter un article',
			'liste' => $liste_article,
		));
	}
}
